package com.smalls.newvideotwo.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.smalls.newvideotwo.R;
import com.smalls.newvideotwo.db.Record;
import com.smalls.newvideotwo.utils.Constant;
import com.smalls.newvideotwo.utils.ImageLoaderUtil;
import com.smalls.newvideotwo.utils.Utils;
import com.smalls.newvideotwo.view.BaseGridView;

import java.util.ArrayList;
import java.util.List;

public class HistoryAdapter<T> extends BaseAdapter {
    private static String TAG = Constant.baddcoolio2tag? Constant.TAGPREFIXSTR + "HistoryAdapter":"HistoryAdapter";
    private ImageLoaderUtil imageLoaderUtil = new ImageLoaderUtil();
    private Context mContext;
    private LayoutInflater mInflater;
    private int type;
    private List<T> vodDatas = new ArrayList();

    static class ViewHolder {
        private TextView movelist_rate;
        private ImageView poster;
        private TextView state;
        private View topicTip;
        private TextView videoName;

        ViewHolder() {
        }
    }

    public HistoryAdapter(Context context, List<T> datas, int type) {
        this.mContext = context;
        this.vodDatas.addAll(datas);
        this.type = type;
        this.mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void changData(List<T> paramArrayList) {
        setVideos(paramArrayList);
        notifyDataSetChanged();
    }

    public void setAppData(List applist) {
        if (applist != null) {
            this.vodDatas = applist;
        }
    }

    private void setVideos(List<T> paramArrayList) {
        if (paramArrayList != null) {
            this.vodDatas = paramArrayList;
        } else {
            this.vodDatas = new ArrayList();
        }
    }

    public void clearDatas() {
        if (this.vodDatas != null && this.vodDatas.size() > 0) {
            this.vodDatas.clear();
        }
    }

    public void remove(int index) {
        if (this.vodDatas != null && this.vodDatas.size() > index) {
            this.vodDatas.remove(index);
        }
    }

    public void remove(T data) {
        if (this.vodDatas != null) {
            this.vodDatas.remove(data);
        }
    }

    public List<T> getDatas() {
        return this.vodDatas;
    }

    private String getEpisodeTitle(String title) {
        if (title == null || title.isEmpty() || !Utils.isNumberic(title)) {
            return title;
        }
        return mContext.getString(R.string.set) + title + mContext.getString(R.string.sets);
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = this.mInflater.inflate(R.layout.item_history_details, null);
            holder = new ViewHolder();
            holder.poster = (ImageView) convertView.findViewById(R.id.new_video_poster);
            holder.state = (TextView) convertView.findViewById(R.id.new_video_state);
            holder.videoName = (TextView) convertView.findViewById(R.id.new_video_name);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if ((parent instanceof BaseGridView) && ((BaseGridView) parent).isOnMeasure) {
            //convertView.setPadding(4, 7, 0, 0);
        } else {
            Record vd = (Record) this.vodDatas.get(position);
            setTitle(holder.videoName, vd);
            if (TextUtils.isEmpty(vd.getAlbumPic())) {
                //holder.poster.setImageResource(R.mipmap.default_poster);
            } else {
                //holder.poster.setImageResource(R.mipmap.default_poster);
                ///this.imageLoaderUtil.loadImage(vd.getAlbumPic(), holder.poster, this.mContext,R.mipmap.default_poster, 5.0f);
            }

            holder.state.setVisibility(View.GONE);
            holder.movelist_rate.setVisibility(View.GONE);
            holder.topicTip.setVisibility(View.VISIBLE);

        }
        return convertView;
    }

    private void setTitle(TextView tv, Record vd) {
        String title = "";
    if (!TextUtils.isEmpty(vd.getTitle())) {
            tv.setText(vd.getTitle());
        }
    }

    public int getCount() {
        return this.vodDatas.size();
    }

    public Object getItem(int position) {
        return this.vodDatas.get((getCount() - 1) - position);
    }

    public long getItemId(int position) {
        return (long) position;
    }
}
